
<template>
  <el-dialog
    :close-on-click-modal="false"
    title="安全隐患信息"
    :visible.sync="show"
    center
  >
    <!-- width="1100px" -->

    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <el-form-item label="隐患编号" prop="number">
        <el-input v-model="form.number" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="隐患等级" prop="level">
        <el-select v-model="form.level" :disabled="disableForm" style="width: 100%;" placeholder="请选择隐患等级">
          <el-option v-for="item in dangerLevels" :key="item.name" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患内容" prop="content">
        <el-input v-model="form.content" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="引发事故类型" prop="type">
        <el-input v-model="form.type" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="判断依据" prop="reason">
        <el-input v-model="form.reason" :disabled="disableForm" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveClassify">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addSecurityRiskDb, updateSecurityRiskDb } from '@/api/securityManage'
import { dangerLevels } from '@/utils/dicData'
export default {
  name: 'AddHiddenDangerClassify',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    relateData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      dangerLevels,
      form: {
        number: '',
        content: '',
        level: '',
        type: '',
        reason: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入安全隐患名称', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        // this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            this.form[key] = this.data[key]
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
        this.targetList = []
      }
    }
  },
  mounted() {
  },
  methods: {
    saveClassify() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            ...this.form,
            ...this.optType === 'edit' ? {
              id: this.data.id
            } : {
            },
            securityRiskCategoryId: this.relateData.id
          }
          const fn = this.optType === 'add' ? addSecurityRiskDb : updateSecurityRiskDb
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              // this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>

